<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>

</body>
<script>
  var arr = function (nums) {
    return Math.max(...nums.reduce((dp, num) => [Math.max(...dp), dp[0] + num], [0, 0]))
  };
  console.log(arr(nums = [1, 2, 3, 1]));
</script>

</html>